package com.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

//工具类
public class MyBatisUtils {

	private static SqlSessionFactory sqlSessionFactory;

	//该代码块固定写法
	static {
		try {
			String resource = "mybatis-config.xml";
			InputStream inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//获取SqlSession连接
	public static SqlSession getSession() {
		// 传入一个Boolean类型的参数，该参数就表示是否自动提交事务
		return sqlSessionFactory.openSession(true);
	}
}
